<?php

class TagModel extends Model{

	public $validate = array(



		);
	//批量添加标签操作
	public function addData(){
		//将用逗号链接的标签转化为数组
		$tnames = explode(',', $_POST['tname']);
		//循环添加标签
		foreach ($tnames as $k => $t) {
			$data = array(
				'tname' => $t,
				);
			//如果该标签已经存在，跳过本次
			if($this->where(array('tname'=>$t))->find()) continue;
			$re = $this->add($data);
			if(!$re) return false;
		}
		return true;
	}
	//获取所有标签数据
	public function getData(){
		return $this->all();
	}

	//后台 文章列表显示 文章已选标签
	public function getTnameByaid($aid){
		$pre = C('DB_PREFIX');
		$sql = "SELECT t.* FROM {$pre}article_tags at JOIN {$pre}tag t ON at.tid=t.tid WHERE at.aid=$aid";

		$tags = M('tag')->query($sql);
		return $tags;
	}

	
}